from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SECRET_KEY'] = '1234'  # 密码
# 设置连接数据库的URL
# 协议：mysql+pymysql
# 用户名：root
# 密码：1234
# IP地址：localhost
# 端口：3306
# 数据库名：flaskmysql #这里的数据库需要提前建好
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:1234@localhost:3306/flask-pyjwt-auth'
# 设置每次请求结束后会自动提交数据库中的改动
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
db = SQLAlchemy(app)


# 查询时会显示原始SQL语句
# app.config['SQLALCHEMY_ECHO'] = True


# 新建表User
class Users(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(250), unique=True, nullable=False)
    username = db.Column(db.String(250), unique=True, nullable=False)
    password = db.Column(db.String(250))
    login_time = db.Column(db.Integer)

    def __init__(self, username, password, email):
        self.username = username
        self.password = password
        self.email = email

    def __str__(self):
        return "Users(id='%s')" % self.id


if __name__ == '__main__':
    db.drop_all()  # 删除存在表
    db.create_all()  # 创建这两个表
    app.run("0.0.0.0", 9527)
    app.config["DEBUG"] = True
